package com.zlx.sharelive.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zlx.module_base.base_api.res_data.MyAuctionTradingBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_fg.BaseFg;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.activity.AuctionDetailsAc;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;
import com.zlx.sharelive.utils.GlideImgManager;
import com.zlx.sharelive.utils.StringUt;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import in.xiandan.countdowntimer.CountDownTimerSupport;
import in.xiandan.countdowntimer.OnCountDownTimerListener;

/**
 * 我的竞拍
 */
public class MyBiddingFg extends BaseFg {

    @BindView(R.id.rv_data)
    RecyclerView rv_data;

    private List<MyAuctionTradingBean.InfoDTO.RecordsDTO> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<MyAuctionTradingBean.InfoDTO.RecordsDTO> mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fg_my_bidding;
    }

    @Override
    protected void initViews() {
        super.initViews();

        myAuctionTrading();
        initRecyclerView();
    }

    private void myAuctionTrading() {
        ApiUtil.getFriendApi().myAuctionTrading(SPUtil.getToken(getContext())).observe(getViewLifecycleOwner(), new BaseObserver<>(new BaseObserverCallBack<ApiResponse<MyAuctionTradingBean>>() {
            @Override
            public void onSuccess(ApiResponse<MyAuctionTradingBean> data) {
                mList.clear();
                if (null != data) {
                    if (null != data.getData()) {
                        if (null != data.getData().getInfo()) {
                            if (data.getData().getInfo().size() > 0) {
                                if (null != data.getData().getInfo().get(0).getRecords()) {
                                    mList.addAll(data.getData().getInfo().get(0).getRecords());
                                }
                            }
                        }
                    }
                }
                mAdapter.notifyDataSetChanged();
            }
        }));
    }


    /**
     * 初始化数据
     */
    private void initRecyclerView() {
        rv_data.setLayoutManager(new LinearLayoutManager(getContext()));

        mAdapter = new CommonRecyclerViewAdapter<MyAuctionTradingBean.InfoDTO.RecordsDTO>(getContext(), mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, MyAuctionTradingBean.InfoDTO.RecordsDTO entity, int position) {
                GlideImgManager.glideLoader(getContext(), entity.getAvatar(), holder.getView(R.id.iv_recommend_head), 0);
                holder.setText(R.id.tv_recommend_nickname, entity.getNickName());
                if (!StringUt.isEmpty(entity.getTime())) {
                    setTime(holder.getView(R.id.tv_time), Long.valueOf(entity.getTime()) * 1000);
                } else {
                    if (entity.getStatus().equals("2")) {
                        //成功 不存在的状态
                        ((ImageView) holder.getView(R.id.iv_state)).setImageResource(R.mipmap.auction_img_true);
                    } else if (entity.getStatus().equals("3")) {
                        //代收款  成功 去支付
                        ((LinearLayout) holder.getView(R.id.ll_time)).setVisibility(View.GONE);
                        ((TextView) holder.getView(R.id.tv_pay)).setVisibility(View.VISIBLE);//支付按钮
                        ((ImageView) holder.getView(R.id.iv_state)).setImageResource(R.mipmap.auction_img_true);
                    } else if (entity.getStatus().equals("4")) {
                        //已收款  成功 待确定
                        ((TextView) holder.getView(R.id.view_time)).setVisibility(View.GONE);
                        ((TextView) holder.getView(R.id.tv_time)).setText("待确定");
                        ((ImageView) holder.getView(R.id.iv_state)).setImageResource(R.mipmap.auction_img_true);
                    } else if (entity.getStatus().equals("5")) {
                        //订单完成 已完成 换图标
                        ((LinearLayout) holder.getView(R.id.ll_time)).setVisibility(View.GONE);
                        ((ImageView) holder.getView(R.id.iv_state)).setImageResource(R.mipmap.auction_img_true);
                    } else if (entity.getStatus().equals("6")) {
                        //失败 切时间结束 换图标
                        ((LinearLayout) holder.getView(R.id.ll_time)).setVisibility(View.GONE);
                        ((ImageView) holder.getView(R.id.iv_state)).setImageResource(R.mipmap.auction_img_false);
                    }
                }
                holder.setText(R.id.tv_up_phone, entity.getUphone());
                holder.setText(R.id.tv_phone, entity.getFriendPhone());
                holder.setText(R.id.tv_money, "￥" + entity.getMoney());
                holder.setText(R.id.tv_time2, entity.getCreateTime());
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_auction_hall;
            }
        };

        rv_data.setAdapter(mAdapter);

        mAdapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                startActivity(new Intent(getContext(), AuctionDetailsAc.class)
                        .putExtra("id", mList.get(position).getId())
                        .putExtra("type", "1"));
            }
        });
    }


    /**
     * 倒计时
     */
    private void setTime(TextView tv_time, long time) {
        //毫秒
        CountDownTimerSupport mTimer = new CountDownTimerSupport(time, 100);
        mTimer.setOnCountDownTimerListener(new OnCountDownTimerListener() {
            @Override
            public void onTick(long millisUntilFinished) {
                tv_time.setText(formatDuring(millisUntilFinished));
            }

            @Override
            public void onFinish() {
                // 倒计时结束
                tv_time.setText("倒计时结束");
            }

            @Override
            public void onCancel() {
                // 倒计时手动停止
            }
        });
        mTimer.start();
    }

    public static String formatDuring(long mss) {

        long days = mss / (1000 * 60 * 60 * 24);

        long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);

        long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);

        long seconds = (mss % (1000 * 60)) / 1000;

        return days + " 天 " + hours + ":" + minutes + ":"

                + seconds;

    }
}
